$(document).ready(function() {
  
  var animating = false,
      submitPhase1 = 1100,
      submitPhase2 = 400,
      logoutPhase1 = 800,
      $login = $(".login"),
      $app = $(".app");
  
  function ripple(elem, e) {
    $(".ripple").remove();
    var elTop = elem.offset().top,
        elLeft = elem.offset().left,
        x = e.pageX - elLeft,
        y = e.pageY - elTop;
    var $ripple = $("<div class='ripple'></div>");
    $ripple.css({top: y, left: x});
    elem.append($ripple);
  };
  
  $(document).on("click", ".login__submit", function(e) {

    if (animating) return;
    animating = true;
    var that = this;
    ripple($(that), e);
    $(that).addClass("processing");
    var _this = $(that);
    setTimeout(function() {
      $.post("/login",{'phone':$("input[name=phone]").val(),'code':$("input[name=code]").val(),'_token':$("input[name=_token]").val()},function(e){
          if(e.id == 0){
              _this.addClass("success");
              setTimeout(function() {
                    location="user";
                  }, submitPhase2);
          }else{
              layer.msg(e.msg);
              animating = false;
              _this.removeClass("processing");
          }
      })
    }, submitPhase1);
  });
  
  $(document).on("click", ".app__logout", function(e) {
    if (animating) return;
    $(".ripple").remove();
    animating = true;
    var that = this;
    $(that).addClass("clicked");
    setTimeout(function() {
        $.get("/logout",{},function(e){
            if(e.id == 0){
                location="login";
            }
        })
    }, logoutPhase1 - 120);
    // setTimeout(function() {
    //   alert(1)
    //   $app.hide();
    //   animating = false;
    //   $(that).removeClass("clicked");
    // }, logoutPhase1);
  });

    $(document).on("click", ".code", function(e) {
        $.post("/sms",{'phone':$("input[name=phone]").val(),'_token':$("input[name=_token]").val()},function(e){
            if(e.error_code == 0){
                layer.msg(e.reason);
            }else{
                layer.msg(e.msg);
            }
        },'json')
    });
});